package com.wyz.saas.common.utils

import android.app.Activity
import android.content.res.Resources
import android.graphics.Rect
import android.os.Build

object UiTool {
    private var STATUS_BAR_HEIGHT = -1
    /**
     * 得到我们的状态栏的高度
     */
    fun getStatusBarHeight(activity: Activity): Int {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && STATUS_BAR_HEIGHT == -1) {
            try {
                val res = activity.resources
                // 尝试获取status_bar_height这个属性的Id对应的资源int值
                var resourceId = res.getIdentifier("status_bar_height", "dimen", "android")
                if (resourceId <= 0) {
                    val clazz =
                        Class.forName("com.android.internal.R\$dimen")
                    val `object` = clazz.newInstance()
                    resourceId =
                        clazz.getField("status_bar_height")[`object`].toString().toInt()
                }
                // 如果拿到了就直接调用获取值
                if (resourceId > 0) STATUS_BAR_HEIGHT = res.getDimensionPixelSize(resourceId)
                // 如果还是未拿到
                if (STATUS_BAR_HEIGHT <= 0) {
                    // 通过Window拿取
                    val rectangle = Rect()
                    val window = activity.window
                    window.decorView.getWindowVisibleDisplayFrame(rectangle)
                    STATUS_BAR_HEIGHT = rectangle.top
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return STATUS_BAR_HEIGHT
    }

    fun getScreenWidth(activity: Activity): Int {
        val displayMetrics = activity.resources.displayMetrics
        //int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        return displayMetrics.widthPixels
    }

    fun getScreenHeight(activity: Activity): Int {
        val displayMetrics = activity.resources.displayMetrics
        //int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        return displayMetrics.heightPixels
    }


    fun getColor(resources: Resources, id: Int): Int {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            resources.getColor(id, null)
        } else {
            resources.getColor(id)
        }
    }
}